<template>
	<view>
		<view class="mapbox">
			<map 
				name="" 
				id="mapself" 
				:latitude="latitude" 
				:longitude="longitude" 
				:markers="markers"
				></map>
		</view>
		<view class="position">
			<view class="locainfo">
				<view class="localeft">
					<image :src="BASE_IMG_URL+'ydw.png'" class="dingwei" mode="scaleToFill"></image>
					<view class="text">{{address}}</view>
				</view>
			</view>
			<view style="background:#fff;">
				<view class="inp_warp">
					<view class="inp_title">拜访客户:</view>
					<picker @change="changeKehu" :disabled="defaultCustomer" :range="kehuList" :range-key="'name'"
						:value="kehuIndex">
						<view class="form_right">
							<view :class="kehuList[kehuIndex] ? 'valueActive' : '' ">
								{{kehuList[kehuIndex] ? kehuList[kehuIndex].name : '点击选择'}}
							</view>
						</view>
					</picker>
				</view>
				<view class="fllow_area">
					<view class="area_head"><text>*</text>备注</view>
					<textarea v-model="content" :class="content ? 'valueActive' : '' " placeholder="请输入备注内容"
						placeholder-style="color:#999" />
				</view>
				<view class="inp_warp">
					<view class="inp_title">图片</view>
					<view class="form_right" @click="uploadFiles" style="color: #333;">
						<view>上传图片</view>
						<view class="iconfont icon-arrows_right"></view>
					</view>
				</view>
				<view class="imglist" v-if="imgList.length != 0">
					<view class="imgbox" v-for="(item,index) in imgList" :key="index">
						<image :src="BASE_IMG_URL+'close.png'" class="delimg" @click="delImg(index)"
							mode="scaleToFill"></image>
							
						<image :src="item.url" class="selfimg" mode="scaleToFill"></image>
					</view>
				</view>
			</view>
			<view style="height:120rpx"></view>
			<view class="savebox">
				<view class="savebtn" @click="$noMultipleClicks(toAdd)">保存</view>
			</view>
			
			
			
			<view class="popmask" v-if="showSelect" @tap.stop="showSelect = false">
				<view class="popbox" @tap.stop.prevent>
					<view class="popsearch">
						<u-search @search="searchName" @clear="searchName" placeholder="请输入关键词搜索"></u-search>
					</view>
					<view class="listwarp">
						<scroll-view scroll-y class="listscroll">
							<view class="liwarp" v-for="(item,index) in kehuList" :key="index"
								@click="selectItem(item,index)">
								<view class="warpleft">
									<view class="name">名称:{{item.name}}</view>
								</view>
								<label class="radio">
									<radio value="r1" :checked="kehuIndex == index" />
								</label>
							</view>
							<view class="noData" v-if="kehuList.length == 0">暂无数据</view>
						</scroll-view>
					</view>
					<view class="btnlist" @tap.stop.prevent>
						<view class="btn firbtn" @click="showSelect = false">确定</view>
						<view class="btn secbtn" @click="showSelect = false">取消</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		BASE_URL,
		SIGN_NO
	} from '@/util/api.js'
	import {
		netAddSignin
	} from '@/api/index.js'
	import {
		netPrevCustomer
	} from '@/api/kehu.js'
	
	let key = uni.getStorageSync('mapKey')
	// #ifndef H5
	var QQMapWX;
	var qqmapsdk;
	if(key){
		QQMapWX = require('../../util/qqmap-wx-jssdk.min.js')
		qqmapsdk = new QQMapWX({
			key: key // 必填
		})
	}
	// #endif
	
	import {
		BASE_IMG_URL
	} from '@/util/api.js'
	export default {
		data() {
			return {
				noClick: true, //防止  重复点击
				longitude: null,
				latitude: null,
				markers: [],
				address: '',
				imgList: [],
				content: '',
				kehuList: [],
				kehuIndex: null,
				kehuName: '',
				showSelect: false,
				BASE_IMG_URL: BASE_IMG_URL,
				location: '', //地址 
				lng: '',
				lat: '',
				files: '',
				customer_id: '',
				defaultCustomer: false,

			}
		},
		onLoad(options) {
			//获取位置
			this.getAddressinfo()
			//获取 客户列表
			this.getKehulist()
			if (options.customerid) {
				console.log(options)
				this.customer_id = options.customerid
				this.defaultCustomer = true
			}
		},
		methods: {
			//设置 默认 客户
			handleCustomer() {
				let id = this.customer_id
				let arr = this.kehuList
				arr.forEach((ele, index) => {
					if (ele.id == id) {
						this.kehuIndex = index
					}
				})
			},
			getKehulist() {
				netPrevCustomer({
					name: this.kehuName
				}).then(res => {
					this.kehuList = res.data
					if (this.defaultCustomer) {
						//设置默认 客户
						this.handleCustomer()
					}
				})
			},
			searchName(e) {
				this.kehuName = e.detail
				this.getKehulist()
			},
			selectItem(item, index) {
				this.kehuIndex = index
			},
			getAddressinfo() {
				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						console.log(res, '获取位置')
						this.longitude = res.longitude
						this.latitude = res.latitude
						this.markers = [{
							id: 1,
							latitude: res.latitude,
							longitude: res.longitude,
							iconPath: BASE_IMG_URL+'weizhi.png',
							width: 30,
							height: 30
						}]
						// #ifndef H5
						//地址逆解析
						if(key) {
							qqmapsdk.reverseGeocoder({
								location: {
									longitude: res.longitude,
									latitude: res.latitude
								},
								success: (re) => {
									this.address = re.result.address
								}
							})
						}else{
							uni.showToast({
								title:'请在后台配置地图key',
								icon:'none'
							})
						}
						// #endif
						// #ifdef H5
						this.$jsonp('https://apis.map.qq.com/ws/geocoder/v1',{
							key:key,
							output:'jsonp',
							location:`${res.latitude},${res.longitude}`,
							get_poi:'1'
						}).then(res=>{
							this.address = res.result.address_component.province+res.result.address_component.city+res.result.formatted_addresses.rough
						})
						// #endif
					},
					fail: (err) => {
						console.log(err, '错误')
					}
				})
			},
			changeKehu(e) {
				this.kehuIndex = e.detail.value
			},
			//上传图片
			uploadFiles() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						let file = res.tempFilePaths[0]
						this.toUpload(file)
					}
				})
			},
			toUpload(file) {
				uni.uploadFile({
					url: BASE_URL + 'common/upload',
					name: 'file',
					header: {
						'token': uni.getStorageSync('token'),
						'sign-no': SIGN_NO
					},
					// #ifdef MP-ALIPAY
					fileType:'image',
					// #endif
					filePath: file,
					success: (res) => {
						let data = JSON.parse(res.data)
						if (data.code == 1) {
							this.imgList.push(data.data)
						} else {
							uni.showToast({
								title: '上传失败',
								icon: 'none'
							})
						}
					},
					fail: (err) => {
						console.log(err)
					}
				})
			},
			delImg(index) {
				this.imgList.splice(index, 1)
			},
			//保存
			toAdd() {
				let {
					longitude,
					latitude,
					address,
					imgList,
					kehuList,
					kehuIndex,
					content
				} = this
				if (!kehuList[kehuIndex]) {
					uni.showToast({
						title: '请选择拜访客户',
						icon: 'none'
					})
					return
				}
				console.log(kehuList[kehuIndex], '0.0')
				if (!content) {
					uni.showToast({
						title: '请填写备注',
						icon: 'none'
					})
					return
				}
				let files = ''
				let arr = []
				imgList.forEach(item => {
					arr.push(item.id)
				})
				files = arr.join(',')
				let params = {
					location: address,
					lng: longitude,
					lat: latitude,
					file_ids: files,
					content,
					customer_id: kehuList[kehuIndex].id
				}
				console.log(params, '保存')
				netAddSignin(params).then(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 2000)
				})
			}
		}
	}
</script>
<style>
	radio {
		transform: scale(0.6);
	}
</style>

<style lang="scss" scoped>
	.popmask {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.5);
		z-index: 2;

		.popbox {
			position: absolute;
			left: 0;
			bottom: 0;
			background: #fff;
			border-top-left-radius: 20rpx;
			border-top-right-radius: 20rpx;
			padding: 20rpx 24rpx;
			width: 750rpx;
			box-sizing: border-box;

			.popsearch {}

			.listwarp {
				width: 702rpx;
				height: 700rpx;

				.listscroll {
					width: 100%;
					height: 700rpx;
					background: #f5f5f5;

					.liwarp {
						width: 580rpx;
						padding: 20rpx 30rpx;
						margin: 20rpx auto;
						background: #fff;
						border-radius: 10rpx;
						display: flex;
						justify-content: space-between;
						align-items: center;

						.warpleft {
							flex: 1;
							display: flex;
							justify-content: flex-start;
							align-items: center;

							.headerimg {
								width: 50rpx;
								height: 50rpx;
								border-radius: 50%;
								margin-right: 15rpx;
							}

							.name {
								font-size: 24rpx;
								color: #333;
							}

							.number {
								font-size: 24rpx;
								color: #999;
								margin: 15rpx 0;
							}

							.guishu {
								font-size: 24rpx;
								color: #666;
							}
						}
					}

					.noData {
						font-size: 26rpx;
						color: #666;
						text-align: center;
						padding-top: 100rpx;
					}
				}
			}

			.btnlist {
				width: 750rpx;
				display: flex;
				justify-content: space-around;
				padding: 30rpx 0;

				.btn {
					width: 200rpx;
					height: 60rpx;
					border-radius: 30rpx;
					font-size: 24rpx;
					color: #fff;
					text-align: center;
					line-height: 60rpx;
				}

				.firbtn {
					background: #03aa96;
				}

				.secbtn {
					background: #ff7800;
				}
			}
		}
	}

	.valueActive {
		color: #333;
	}

	.mapbox {
		width: 100%;
		height: 400rpx;
		#mapself {
			width: 100%;
			height: 400rpx;
		}
	}

	.locainfo {
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: #fff;

		.localeft {
			display: flex;
			justify-content: flex-start;
			align-items: center;

			.dingwei {
				width: 44rpx;
				height: 44rpx;
				margin-right: 15rpx;
			}

			.text {
				font-size: 26rpx;
				color: #333;
			}
		}

		.locaright {
			image {
				width: 46rpx;
				height: 46rpx;
			}
		}
	}

	.inp_warp {
		padding: 20rpx 0;
		background: #fff;
		margin-top: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1rpx solid #CCC;
		margin-bottom: 30rpx;
		border-top: 1px solid #ededed;

		.inp_title {
			font-size: 32rpx;
			color: #999;
			padding-left: 70rpx;
		}

		.form_right {
			display: flex;
			align-items: center;
			justify-content: flex-end;
			color: #333;
			font-size: 32rpx;
			padding-right: 30rpx;
		}
	}

	.fllow_area {
		background-color: #fff;
		padding: 0 70rpx;
		padding-bottom: 30rpx;
		color: #999;

		.area_head {
			height: 40rpx;
			line-height: 40rpx;
			font-size: 32rpx;
			margin-bottom:24rpx;
			
			text {
				color: #f00;
			}
		}

		textarea {
			width: 600rpx;
			height: 250rpx;
			padding: 30rpx;
			box-sizing: border-box;
			background: #f8f8f8;
			border-radius: 15rpx;
			margin: 0 auto;
		}
	}

	.savebox {
		display: flex;
		justify-content: center;
		align-items: center;
		position: fixed;
		left:0;
		bottom:0;
		width:750rpx;
		height:100rpx;
		background:#fff;
		z-index: 2;
		.savebtn {
			font-size: 26rpx;
			color: #fff;
			text-align: center;
			line-height: 88rpx;
			width: 500rpx;
			height: 88rpx;
			background: #03AA96;
			border-radius: 29rpx;
		}
	}

	.imglist {
		padding: 24rpx 34rpx;
		background: #fff;
		border-bottom: 1rpx solid #EAEAEA;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		flex-wrap: wrap;
		.imgbox {
			width: 160rpx;
			height: 160rpx;
			margin-right: 14rpx;
			position: relative;
			margin-bottom: 24rpx;
			border: 1rpx solid #EAEAEA;
			.delimg {
				width: 32rpx;
				height: 32rpx;
				border-radius: 50%;
				position: absolute;
				right: -16rpx;
				top: -16rpx;
				background: rgba(0, 0, 0, 0.5);
				z-index: 2;
			}
			.selfimg {
				width: 160rpx;
				height: 160rpx;
			}
		}
	}
</style>
